/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;


import javax.swing.JTextField;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;

/**
 * A JTextField ( javax.swing.JTextField) that conforms to the SpaglCoreField interface
 * @author Sam Pottinger
 */
public class SpaglField extends JTextField implements spagcore.SpaglCoreField {

	private static final long serialVersionUID = 1L;
	private SpaglCoreContainer parentWidget;
	private SpaglCoreFont widgetFont;
	
	/**
	 * Creates a new text field using the default size and placement behaviors
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 */
	public SpaglField(SpaglCoreContainer newParent)
	{
		super();
		parentWidget = newParent;
	}
	
	/**
	 * Creates a new text field using a specified width and default placement behavior
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 * @param width The integer width of this text field
	 */
	public SpaglField(SpaglCoreContainer newParent, int width)
	{
		super(width);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a new text field using a specified width and default placement behavior
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 * @param width The integer width of this text field
	 * @param parameters The sting parameters to be used for the layout manager
	 */
	public SpaglField(SpaglCoreContainer newParent, int width, String parameters)
	{
		super(width);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}
	
	/**
	 * Gets the parent container that is holding this widget
	 * @return This object's parent container as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the font currently used by this widget
	 * @return The SpaglFont used by this widget as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 * @param dispose True if the font should be disposed of immediately (only simulation on Swing)
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		if(!dispose)
			widgetFont = newFont;
		else
			widgetFont = null;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}
	
	/**
	 * Adds an event listener to this field
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try {
		switch(eventID)
		{
			case SpaglEvent.TEXT_CHANGED_EVENT: {this.getDocument().addDocumentListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.TEXT_CHANGED_EVENT)); break;}
			default: throw new IllegalArgumentException("Event not supported by this widget");
		}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
	
}
